<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/7/23
 * Time: 17:50
 */

namespace app\api\controller;


use app\api\controller\Base;
use app\api\model\Goods as C;
use think\Controller;
use app\api\model\Coupon as M;
use think\Request;

class Goods extends Base
{

    public function index(){
        $c=new C();
        $city_id= request()->post('city_id');
        $city_id=!empty($city_id)?$city_id:0;
        $id=input('classify_id');
        $openid=request()->post('openid');
        $id=!empty($id)?$id:0;
        $lat=input('lat');
        $lng=input('lng');
        $score_sort1= input('score_sort');
        $distance_sort1= input('distance_sort');
        $score_sort=!empty($score_sort1)?$score_sort1:'1';//默认为1
        $distance_sort=!empty($distance_sort1)?$distance_sort1:'1';//默认为1

        $post = request()->post();

        //分页
        $page = !empty($post['page']) ? $post['page'] : 1;
        $pageLimit = !empty($post['pagelimit']) ? $post['pagelimit'] : 100;

        $res_data=$c->program_classifypoi($id,$city_id,$lat,$lng,$openid,$page,$pageLimit);
        if ($res_data==true){
            $data = $res_data['data'];
            $data1 = $res_data['data1'];

            if ($score_sort!="1"){
                $data= $this->insertSort($data,'score');
            }
            if ($distance_sort!="1"){
                $data= $this->insertSort($data,'juli');
            }

            $shu=[
                'code'=>0,
                'msg'=>'请求成功',
                'data'=>$data,
                'pageArr'=>$data1,
            ];
        }else{
            $shu=[
                'code'=>0,
                'msg'=>'请求失败',
                'data'=>[]
            ];
        }

        return json_encode($shu,JSON_UNESCAPED_UNICODE);
    }

    public function goodsdetails(){
        $m=new C();
        $poi_id=request()->post('goods_id');
        $openid=request()->post('openid');
        $data= $m->goodsdetails($poi_id,$openid);
        return json_encode($data);
    }

    public function package(){//套餐
        $goods_id= input('goods_id');
        $specs_id= input('specs_id');
        $openid= input('openid');

        $exchange= db('exchange')->where("currency_code","USD")->find();//汇率

        $goods=db('goods')->where('goods_id',$goods_id)->find();
        if (empty($goods)){
            $shu=[
                'code'=>101,
                'msg'=>'参数错误',
            ];
            echo json_encode($shu);die();
        }
        if(!empty($goods['code'])){
            $Viator= new Viator();
            $month=date('m',time());
            $jia=[];
            $data=[];
            $d=[];
            $data1= $Viator->getPriceShow($goods['code'],$month);
            foreach ($data1 as $s=>$v){
                $d[$s]['shijian']=$v['bookingDate'];
                $d[$s]['shuliang']=999;
                foreach ($v['ageBands'] as $l=>$k){

                    $jia[]=$k['price'];
                    $d[$s]['Person'][$l]=[
                        "person_id"=>$k['bandId'],
                        "person_price"=>ceil($k['price']*$exchange['exchange_rate']),
                        "person_num"=>999,
                        "person_name"=>$k['age'].'岁'.$Viator->etoc($k['description']),
                    ];
                }
                $min= max($jia);

                $d[$s]['jiage']=ceil($min*$exchange['exchange_rate']);

            }

            foreach ($d as $v){
                $data['riqijiage'][]=$v;
            }
            $data['specs'][]=[
                "specs_id"=>'-',
                "specs_name"=>"普通套餐",
                "specs_types"=> "1"
            ];
            $shu=[
                'code'=>0,
                'msg'=>'ko',
                'data'=>$data
            ];
            echo json_encode($shu);die;
        }
        //套餐选择头部数据
        $m= new C();
        $data= $m->package($goods_id,$specs_id,$openid);
        if ($data==true){
            $shu=[
                'code'=>0,
                'msg'=>'请求成功',
                'data'=>$data
            ];
        }else{
            $shu=[
                'code'=>0,
                'msg'=>'请求失败',
                'data'=>[]
            ];
        }
        return json_encode($shu);

    }


    //排序
    public function insertSort($arr,$c) {
        $len=count($arr);
        if ($c=='juli'){
            for($i=1; $i<$len; $i++ ) {
                $tmp = $arr[$i];
                //内层循环控制，比较并插入
                for($j=$i-1;$j>=0;$j--) {
                    if($tmp[$c] < $arr[$j][$c]) {
                        //发现插入的元素要小，交换位置，将后边的元素与前面的元素互换
                        $arr[$j+1] = $arr[$j];
                        $arr[$j] = $tmp;
                    } else {
                        //如果碰到不需要移动的元素，由于是已经排序好是数组，则前面的就不需要再次比较了。
                        break;
                    }
                }
            }
            return $arr;
        }else{
            for($i=1; $i<$len; $i++ ) {
                $tmp = $arr[$i];
                //内层循环控制，比较并插入
                for($j=$i-1;$j>=0;$j--) {
                    if($tmp[$c] > $arr[$j][$c]) {
                        //发现插入的元素要小，交换位置，将后边的元素与前面的元素互换
                        $arr[$j+1] = $arr[$j];
                        $arr[$j] = $tmp;
                    } else {
                        //如果碰到不需要移动的元素，由于是已经排序好是数组，则前面的就不需要再次比较了。
                        break;
                    }
                }
            }
            return $arr;
        }

    }




}



